 //
//  AppDelegate.swift
//  微博08
//
//  Created by apple on 16/2/26.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

/*
    信息安全
    Unity3d: 游戏开发c#
    服务器开发语言: 1.java 2.php 3. .net c# c++ js
*/

/* 
    Swift产品名称不能是中文开头,如果中文开头,创建的对象都有问题,不能添加到数组中
    Swift产品名称不能包含 -, 包含 - 会导致OC调用不了Swift
*/

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setupAppearance()
        
        let account = CZUserAccountViewModel.sharedInstance.userAccount
        print("我在appDelegate里面获取到了账户: \(account)")
        
        // 1.创建window
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 2.设置window的根控制器
        window?.rootViewController = defaultController()
        
        // 3.设置成为主窗口,并显示
        window?.makeKeyAndVisible()
        
        // 判断是否登录
        CZUserAccountViewModel.sharedInstance.userLogin
        
        return true
    }
    
    /// 设置外观颜色
    private func setupAppearance() {
        let item = UIBarButtonItem.appearance()
        item.tintColor = UIColor.orangeColor()
    }
    
    /// 设置默认控制器
    private func defaultController() -> UIViewController {
        if !CZUserAccountViewModel.sharedInstance.userLogin {
            // 用户没有登录,返回没有登录的tabBar
            return CZMainViewController()
        }
        
        // 用户已经登录
        return isNewVersion() ? CZNewFeatureViewController() : CZWelcomeViewController()
    }
    
    // 目标控制器:目标控制器有2个
    /**
    跳转界面
    
    - parameter isMain: true: CZMainViewController, false: CZWelcomeViewController
    */
    private func switchRootViewController(isMain: Bool) {
        window?.rootViewController = isMain ? CZMainViewController() : CZWelcomeViewController()
    }
    
    /**
     跳转界面,外部调用
     
     - parameter isMain: true: CZMainViewController, false: CZWelcomeViewController
     */
    class func outSwitchRootViewController(isMain: Bool) {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.switchRootViewController(isMain)
    }
    
    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 1.获取当前运行的版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        // 2.获取上一次运行的版本
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        
        // 3.对比
        let newVersion = version != sandboxVersion
        
        // 4.是新版,将新版本的版本号保存起来
        if newVersion {
            // 将当前版本号保存起来
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        // 5.返回比较的结果
        return newVersion
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

